const path = require('path')
const chalk = require('chalk')
const fs = require('fs')

const {
  configPath,
  version,
  port = 8080,
  name: platformName,
  publicPath,
  outputDir,
  indexPath,
  baseUrl,
  isBuild,
  theme = '#42bd56',
  template = 'public/index.html',
  title = '农业大数据平台',
  keywords = '',
  description = '',
} = require('./build/platform')
const proxy = require('./build/proxy')(baseUrl)
const cdn = require('./build/cdn')(baseUrl, isBuild)
const autoPlugins = require('./build/auto-plugins')
const VersionPlugin = require('./build/plugins/version')
const Utils = require('./build/utils')

const nextVersion = isBuild ? Utils.getNextVersion(version) : version
let info = `> platform info:
  - version:   ${nextVersion}${isBuild ? '(next)' : ''}
  - name:      ${platformName}
  - title:     ${title}
  - theme:     ${theme}
  - port:      ${port}
  - baseUrl:   ${baseUrl}`
if (isBuild) {
  info += `
  - outputDir: ${outputDir}
  - indexPath: ${indexPath}`
}
console.warn(chalk.blue(info))

module.exports = {
  publicPath,
  outputDir,
  indexPath,
  productionSourceMap: false,
  transpileDependencies: [/[/\\]node_modules[/\\]@vimi[/\\]/],

  lintOnSave: !isBuild,
  devServer: {
    proxy,
    port,
    https: baseUrl.startsWith('https'),
    open: true,
    overlay: {
      warnings: false,
      errors: true,
    },
  },

  configureWebpack: config => {
    config.entry.app = [`./src/${platformName}/main.js`]
    config.plugins.push(
      new VersionPlugin({
        configPath,
        version: nextVersion,
        theme,
        isBuild,
      })
    )

    if (isBuild) {
      // config.devtool = 'cheap-module-source-map'
      config.externals = cdn.externals
      config.plugins.push(...autoPlugins)
    } else {
      config.devtool = 'eval-source-map'
    }
  },

  chainWebpack: config => {
    config.plugin('html').tap(args => {
      const options = args[0]
      options.cdn = {
        css: cdn.css,
        js: isBuild ? cdn.js : [],
      }
      options.template = template
      options.title = title
      options.meta = {
        keywords,
        description,
      }
      return args
    })

    // 移除 prefetch 插件
    config.plugins.delete('prefetch')
    // 移除 preload 插件
    config.plugins.delete('preload')

    // 替换静态文件夹路径
    // 各项目的静态文件以项目名命名，还是放在根目录中的public文件夹中
    // img可以直接引用：<img src="/bigdata/img/img.jpg">
    // 开发可正常显示，发布到线上的路径也是正确的
    // 但要注意，开发时引用其他项目的静态文件时，如果其他项目没有发布，线上会找不到资源
    const copyPath = path.resolve(__dirname, 'public', platformName)
    const exists = fs.existsSync(copyPath)
    if (exists) {
      config.plugin('copy').tap(args => {
        args[0][0].from = copyPath
        return args
      })
    } else {
      config.plugins.delete('copy')
    }
  },

  css: {
    loaderOptions: {
      sass: {
        // data选项会将它拼接在入口文件的内容之前
        // 如果是样式，build后也会每个组件都有，所以最好只是变量和方法的定义
        data: `
        $primary: ${theme};
        @import "@/${platformName}/assets/scss/_variables.scss";
        @import "@/common/assets/scss/_variables.scss";
        @import "@/common/assets/scss/_mixins.scss";
        `,
      },
    },
  },
}
